package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ConfigHouseBlock;
import com.ruoyi.system.service.IConfigHouseBlockService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商业区管理Controller
 * 
 * @author YuKunXu
 * @date 2024-02-27
 */
@RestController
@RequestMapping("/system/block")
public class ConfigHouseBlockController extends BaseController
{
    @Autowired
    private IConfigHouseBlockService configHouseBlockService;

    /**
     * 查询商业区管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:block:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConfigHouseBlock configHouseBlock)
    {
        startPage();
        List<ConfigHouseBlock> list = configHouseBlockService.selectConfigHouseBlockList(configHouseBlock);
        return getDataTable(list);
    }

    /**
     * 导出商业区管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:block:export')")
    @Log(title = "商业区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConfigHouseBlock configHouseBlock)
    {
        List<ConfigHouseBlock> list = configHouseBlockService.selectConfigHouseBlockList(configHouseBlock);
        ExcelUtil<ConfigHouseBlock> util = new ExcelUtil<ConfigHouseBlock>(ConfigHouseBlock.class);
        util.exportExcel(response, list, "商业区管理数据");
    }

    /**
     * 获取商业区管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:block:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(configHouseBlockService.selectConfigHouseBlockById(id));
    }

    /**
     * 新增商业区管理
     */
    @PreAuthorize("@ss.hasPermi('system:block:add')")
    @Log(title = "商业区管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConfigHouseBlock configHouseBlock)
    {
        return toAjax(configHouseBlockService.insertConfigHouseBlock(configHouseBlock));
    }

    /**
     * 修改商业区管理
     */
    @PreAuthorize("@ss.hasPermi('system:block:edit')")
    @Log(title = "商业区管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConfigHouseBlock configHouseBlock)
    {
        return toAjax(configHouseBlockService.updateConfigHouseBlock(configHouseBlock));
    }

    /**
     * 删除商业区管理
     */
    @PreAuthorize("@ss.hasPermi('system:block:remove')")
    @Log(title = "商业区管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        System.out.println("ids =================== " + ids);
        int i = configHouseBlockService.deleteConfigHouseBlockByIds(ids);
        if (i == -1) {
            return AjaxResult.warn("商业区有相关楼栋，不可删除！");
        }
        return toAjax(i);
    }

    /**
     * 商区和楼栋的树形数据
     * 前端所需的树形结构数据
     */
    @PreAuthorize("@ss.hasPermi('system:block:query')")
    @GetMapping("/treelist")
    public AjaxResult getTreeDate()
    {
        return success(configHouseBlockService.selectTreeList());
    }

}
